package com.stox.ui.javafx.charting.draw.shape;

import javafx.beans.value.ChangeListener;
import javafx.beans.value.ObservableValue;
import javafx.collections.ListChangeListener.Change;
import javafx.collections.ObservableList;

import com.stox.ui.javafx.charting.widget.ResizableCanvas;
import com.sun.javafx.collections.TrackableObservableList;

public class DragPoint extends ControlPoint {

	private boolean listenersActive = true;
	
	private final ObservableList<ControlPoint> controlPoints = new TrackableObservableList<ControlPoint>(){
		@Override
		protected void onChanged(Change<ControlPoint> change) {
			
		}
	};
	
	public DragPoint(ResizableCanvas canvas) {
		super(canvas);
		xProperty().addListener(new ChangeListener<Number>(){
			@Override
			public void changed(ObservableValue<? extends Number> observable,
					Number oldValue, Number newValue) {
				if(listenersActive && null != oldValue && null != newValue){
					double change = oldValue.doubleValue() - newValue.doubleValue();
					if(0 != change){
						for(ControlPoint controlPoint : controlPoints){
							controlPoint.setX(controlPoint.getX() - change);
						}
					}
				}
			}
		});
		
		yProperty().addListener(new ChangeListener<Number>(){
			@Override
			public void changed(ObservableValue<? extends Number> observable,
					Number oldValue, Number newValue) {
				if(listenersActive && null != oldValue && null != newValue){
					double change = oldValue.doubleValue() - newValue.doubleValue();
					if(0 != change){
						for(ControlPoint controlPoint : controlPoints){
							controlPoint.setY(controlPoint.getY() - change);
						}
					}
				}
			}
		});
	}
	
	public ObservableList<ControlPoint> getControlPoints() {
		return controlPoints;
	}
	
	public void setListenersActive(boolean listenersActive) {
		this.listenersActive = listenersActive;
	}
	
	public boolean isListenersActive() {
		return listenersActive;
	}

}
